package cn.edu.nju.ws.sview.views.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;

import cn.edu.nju.ws.sview.cache.UserNameIDCache;
import cn.edu.nju.ws.sview.database.DBConnectionFactory;

/**
 * 管理一下collection这中实体有别于单实体的任务
 * @author Saisai Gong
 *
 */
public class CollectionManagement extends Management{
	public final static String CLEAR_DM_IN_COLLECTION_POPULARITY = "clear_dm_in_collection_pop";

	private static CollectionManagement INSTANCE = null;

	private CollectionManagement() {

	}

	public static synchronized CollectionManagement getInstance() {
		if (INSTANCE == null)
			INSTANCE = new CollectionManagement();
		return INSTANCE;
	}

	/**
	 * 存储浏览集合时使用datamember的频率
	 * @param userName
	 * @param dmtype
	 * @param dmid
	 */
	public static void  storeUserDMVisit(String userName,int dmtype,int dmid,boolean inverse) {
		int userID = 0;
		if (userName != null) {
			try {
				userID = UserNameIDCache.getInstance().getUserID(userName);
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		int status = checkStatus(userID, Management.CONTROL_DM_IN_COLLECTION_TYPE,dmtype,dmid);
		if (status != CollectionManagement.VISITED_COUNT_INVALID) {
			ViewManagement.storeVisitInControl(userID, Management.CONTROL_DM_IN_COLLECTION_TYPE, dmtype, dmid);
			userDMVisit(userID, dmtype,dmid,inverse);
		}
	}
	
	private static void userDMVisit(int userID,int dmtype,int dmid,boolean inverse){
		 try {
				Connection con = DBConnectionFactory.getConnection();
				int date = Calendar.getInstance().get(Calendar.DAY_OF_WEEK)-1;
				PreparedStatement user_day_visit_stmt = con.prepareStatement(
						"insert into dmInCollection_day_visit(user_id,member_type,member_id,inverse,date,times_recent) " +
						"values(?,?,?,?,?,?) on duplicate key update times_recent=times_recent+1");
				user_day_visit_stmt.setInt(1, userID) ;
				user_day_visit_stmt.setInt(2, dmtype);
				user_day_visit_stmt.setInt(3, dmid) ;
				user_day_visit_stmt.setBoolean(4, inverse) ;
				int times = 1;
				user_day_visit_stmt.setInt(5,date);
				user_day_visit_stmt.setInt(6, times) ;
				user_day_visit_stmt.executeUpdate();
				user_day_visit_stmt.close();
				
				PreparedStatement user_visit_stmt = con.prepareStatement(
						"insert into dmInCollection_visit(user_id,member_type,member_id,inverse,times_total) " +
						"values(?,?,?,?) on duplicate key update times_total=times_total+1");
				user_visit_stmt.setInt(1, userID) ;
				user_visit_stmt.setInt(2, dmtype);
				user_visit_stmt.setInt(3, dmid) ;
				user_visit_stmt.setBoolean(4, inverse) ;
				user_visit_stmt.setInt(5, times) ;
				user_visit_stmt.executeUpdate();
				user_visit_stmt.close();
				
				con.close();
				
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	public void clearViewPopularity(int viewID) {
		try {
			Connection con = DBConnectionFactory.getConnection();
			Statement stmt = con.createStatement();
			stmt.executeUpdate(String.format(
					"update view set use_times=0 where id=%d ", viewID));
			stmt.close();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void clearViewPopularityAll() {
		try {
			Connection con = DBConnectionFactory.getConnection();
			Statement stmt = con.createStatement();
			stmt.executeUpdate("update view set use_times=0");
			stmt.close();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 删除一个以viewID标识的view的所有相关信息
	 * 
	 * @param viewID
	 *            . view id
	 * @return. 删除是否完全成功
	 */
	public boolean deleteView(int viewID) {
		boolean flag = true;


		try {
			Connection con = DBConnectionFactory.getConnection();
			Statement stmt = con.createStatement();

			stmt.executeUpdate(String.format(
					"delete from class_views where viewId=%d", viewID));
			stmt.executeUpdate(String.format(
					"delete from user_view_visit_control where view_id=%d",
					viewID));
			stmt.executeUpdate(String.format(
					"delete from view_dm where view_id=%d", viewID));
			stmt.executeUpdate(String.format(
					"delete from view_favor where view_id=%d", viewID));
			stmt.executeUpdate(String.format(
					"delete from view_trash where view_id=%d", viewID));
			stmt.executeUpdate(String.format(
					"delete from view_ref where from_view_id=%d", viewID));
			stmt.executeUpdate(String.format(
					"delete from user_view where view_id=%d", viewID));
			stmt.executeUpdate(String.format(
					"delete from view_ref where to_view_id=%d", viewID));
			stmt.executeUpdate(String.format("delete from view where id=%d",
					viewID));

			stmt.close();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			flag = false;
			e.printStackTrace();
		}

		return flag;
	}
}
